#include "musicslider.h"
#include "ui_musicslider.h"
#include<QDebug>
MusicSlider::MusicSlider(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MusicSlider)
{
    ui->setupUi(this);
    currentPos = 0;
    maxWidth = this->width();
    moveSlider();
}

void MusicSlider::setStep(float bf)
{
        currentPos = maxWidth * bf;
        moveSlider();
}


void MusicSlider::mousePressEvent(QMouseEvent *event)
{

       currentPos = event->pos().x();
       moveSlider();
}

void MusicSlider::mouseMoveEvent(QMouseEvent *event)
{
    QRect rect = QRect(0, 0, width(), height());
    QPoint pos = event->pos();
     if(!rect.contains(pos))
     {
        return;
     }
     if(event->buttons() == Qt::LeftButton)
      {
         currentPos = event->pos().x();
          if(currentPos < 0)
          {
            currentPos = 0;
          }
          if(currentPos > maxWidth)
          {
             currentPos = maxWidth;
          }
          moveSlider();
     }
}
 void MusicSlider::mouseReleaseEvent(QMouseEvent *event)
 {    
             currentPos = event->pos().x();
             moveSlider();
             emit setMusicSliderPosition((float)ui->outLine->width()/(float)maxWidth);
 }

 void MusicSlider::resizeEvent(QResizeEvent *event)
 {
     QWidget::resizeEvent(event);
         maxWidth = this->width();
         currentPos = qBound(0, currentPos, maxWidth);
         moveSlider();
 }
 
 void MusicSlider::moveSlider()
 {   
     ui->outLine->setMaximumWidth(currentPos);
     ui->outLine->setGeometry(ui->outLine->x(),ui->outLine->y(), currentPos, ui->outLine->height());
 }
 
MusicSlider::~MusicSlider()
{
    delete ui;
}
